<template>
  <el-aside class="sidebar" :width="width">
    <!-- 页面LOGO -->
    <div class="sidebar-logo">
      <!-- <img src="../../assets/img/qq_speed1.jpg" alt v-show="!isClose" @click="backHome"> -->
    </div>
    <!-- 页面菜单 -->
    <el-menu
      class="menu"
      v-show="!isClose"
      :default-active="defaultActive"
      @select="menuSelectHandle"
    >
      <el-menu-item
        class="menu-tab"
        index="home"
        :style="tabStyle()"
        @click="homeBtnClick"
      >
        <span slot="title">
          <router-link class="link_style" to="/home">首页</router-link>
        </span>
      </el-menu-item>
      <!-- 循环显示一级菜单 -->
      <el-menu-item
        class="menu-tab"
        v-for="value in pageList"
        :key="value.id"
        :index="value.id"
        :style="tabStyle()"
      >
        <span slot="title">
          <!-- {{value.name}} -->
          <router-link class="link_style" :to="showSelf(value.id)">{{
            value.name
          }}</router-link>
        </span>
      </el-menu-item>
      <el-menu-item
        class="menu-tab"
        index="backstage-manage"
        :style="tabStyle()"
        v-if="role === '0'"
      >
        <span slot="title">
          <router-link class="link_style" to="/backstage-manage"
            >后台管理</router-link
          >
        </span>
      </el-menu-item>
    </el-menu>
  </el-aside>
</template>

<script>
// 菜单数据已经异步public/data/menu.json中，请去json中文件配置

export default {
  name: 'view-menu',
  data() {
    return {
      // 菜单结构
      pageList: [],
      // 默认宽度
      defaultWidth: 250,
      // 实时宽度
      width: '150px',
      // 关闭状态
      isClose: false
    }
  },
  // 计算变量
  computed: {
    // 菜单选中
    defaultActive() {
      const arr = this.$route.path.split('/')
      return arr[arr.length - 1]
    },
    role() {
      if (this.$store.state.user) return this.$store.state.user.role
      else return ''
    }
  },
  mounted() {
    this.getall()
  },
  methods: {
    // 可跳转tab样式
    tabStyle() {
      return {
        paddingLeft: '20px'
      }
    },
    // 所有帖子
    async getall() {
      console.log(989898989)
      const res = await this.$axios.get('/article/types/')
      console.log(res, '返回来数据')
    },
    menuSelectHandle(val) {
      console.log(val)
      this.$emit('menuSelectHandle', val)
    },
    // 回到主页
    backHome() {
      this.$router.push({ path: '/' })
    },
    // 更改右侧home参数
    showSelf(id) {
      return `/home/${id}`
    },
    homeBtnClick() {
      this.$emit('homeBtnClick')
    }
  },
  mounted() {
    this.pageList = JSON.parse(localStorage.getItem('sectionList'))
  }
}
</script>

<style lang="scss" scoped>
.sidebar {
  position: relative;
  // height: 100%;
  background-color: rgba(9, 34, 45, 0.4);
  box-shadow: 3px 10px 10px rgba(0, 0, 0, 0.3);
  overflow: unset;
  // transition: all 0.1s;
  z-index: 10;
  .sidebar-logo {
    text-align: center;
    height: 30px;
    img {
      height: 30px;
      border-radius: 50%;
    }
  }
  .link_style {
    margin-left: -20px;
    margin-right: -20px;
    padding: 0 20px;
  }
}

.close {
  position: absolute;
  left: 100%;
  top: 0;
  width: 40px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  font-size: 20px;
  color: #38baf3;
  background: rgba(9, 34, 45, 0.4);
  border: none;
  border-radius: 0 5px 5px 0;
  padding: 0;
}

.close >>> i {
  display: block;
  height: 100%;
  line-height: inherit;
}

.close:hover,
.close:focus {
  background: rgba(9, 34, 45, 0.4);
  color: #38baf3;
}

img {
  margin-top: 20px;
  margin-bottom: 20px;
  cursor: pointer;
}

.menu {
  width: 100%;
  text-align: left;
  font-size: 20px;
  background-color: rgba(9, 34, 45, 0);
  border: none;
}

.menu /deep/ .el-submenu__title {
  height: 37px;
  line-height: 37px;
  color: white;
}

.menu /deep/ .el-submenu__title i {
  color: #38baf3;
}

.menu /deep/ .el-submenu__title:hover {
  background: rgba(5, 31, 58, 0.2);
  border-left: 3px solid #38baf3;
}

.menu-expand /deep/ .el-menu {
  background-color: rgba(255, 0, 0, 0);
}

.menu-expand /deep/ .el-menu-item-group__title {
  padding: 0;
}

.menu /deep/ .menu-tab {
  height: 37px;
  line-height: 37px;
  padding-left: 35px;
  color: #e2e3e5;
}

.menu-tab:hover,
.is-active[tabindex='-1'] {
  color: #38baf3;
  border-left: 3px solid #38baf3;
  background: rgba(5, 31, 58, 0.2);
}

.menu-tab:focus {
  background: rgba(5, 31, 58, 0.2);
}

.el-menu-item * {
  display: block;
}
</style>
